using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace spaceshooter
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        bool usingGamePad;
        enum gameState {titleScreen, menu };
        Texture2D title;
        SpriteFont font;
        gameState currentState;
        plane player, enemy;
        
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferHeight = 720;
            graphics.PreferredBackBufferWidth = 1280;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            base.Initialize();
            if (GamePad.GetState(PlayerIndex.One).IsConnected) usingGamePad = true;
            else usingGamePad = false;

            currentState = gameState.titleScreen;
        }
        
        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            spriteBatch = new SpriteBatch(GraphicsDevice);
            title = Content.Load<Texture2D>("title");
            font = Content.Load<SpriteFont>("SpriteFont1");
            player = new plane(Content.Load<Texture2D>("player"));
            enemy = new plane(Content.Load<Texture2D>("player"));
            player.setWeapon(Content.Load<Texture2D>("pulse laser"));
            player.setPos(500, 500);
            Random r = new Random();
            enemy.setPos(r.Next(0, 1280), r.Next(0, 720));
            // TODO: use this.Content to load your game content here
        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                this.Exit();

            // TODO: Add your update logic here

            switch (currentState)
            {
                case gameState.titleScreen:
                    if (usingGamePad)
                    {
                        if (GamePad.GetState(PlayerIndex.One).Buttons.Start == ButtonState.Pressed) currentState = gameState.menu;
                    }
                    else
                    {
                        if (Keyboard.GetState().IsKeyDown(Keys.Space) == true) currentState = gameState.menu;

                    }
                    break;
                case gameState.menu:
                    if (usingGamePad)
                    {
                        if (GamePad.GetState(PlayerIndex.One).Buttons.Start == ButtonState.Pressed) currentState = gameState.menu;
                    }
                    else
                    {
                        if (Keyboard.GetState().IsKeyDown(Keys.Left) == true) if (!(player.getX() - 10 < 0)) player.setPos(player.getX() - 10, player.getY());
                        if (Keyboard.GetState().IsKeyDown(Keys.Right) == true) if (!((player.getX() -25 + player.getImage().Width) > graphics.PreferredBackBufferWidth)) player.setPos(player.getX() + 10, player.getY());
                        if (Keyboard.GetState().IsKeyDown(Keys.Up) == true) if (!(player.getY() - 10 < 0)) player.setPos(player.getX(), player.getY()-10);
                        if (Keyboard.GetState().IsKeyDown(Keys.Down) == true) if (!((player.getY() -25 + player.getImage().Height) > graphics.PreferredBackBufferHeight)) player.setPos(player.getX(), player.getY()+10);
                        if ((int)gameTime.TotalGameTime.TotalMilliseconds % 10 == 0)
                        {
                            Random r = new Random();
                            if (r.Next(0, 2) == 0) if (!(enemy.getX() - 10 < 0)) enemy.setPos(enemy.getX() - 10, enemy.getY());
                                else if (!((enemy.getX() - 25 + enemy.getImage().Width) > graphics.PreferredBackBufferWidth)) enemy.setPos(enemy.getX() + 10, enemy.getY());

                            if (r.Next(0, 2) == 0) if (!(enemy.getY() - 10 < 0)) enemy.setPos(enemy.getX(), enemy.getY() - 10);
                                else if (!((enemy.getY() - 25 + enemy.getImage().Height) > graphics.PreferredBackBufferHeight)) enemy.setPos(enemy.getX(), enemy.getY() + 10);
                        }

                        if ((int)gameTime.TotalGameTime.TotalMilliseconds % 100 == 0) if (Keyboard.GetState().IsKeyDown(Keys.Space) == true) player.startFiring();

                    }                   
                    break;
            }



            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            System.Diagnostics.Debug.WriteLine(currentState);

            spriteBatch.Begin();

            switch(currentState){
                case gameState.titleScreen:
                    spriteBatch.Draw(title, new Vector2(0, 0), null, Color.White, 0, new Vector2(0, 0), 0.6666666666667f, SpriteEffects.None, 0);
                    if ((int) gameTime.TotalGameTime.TotalSeconds % 2 == 0)
                    {
                        if (usingGamePad) spriteBatch.DrawString(font, "Press A to continue...", new Vector2(640 - ((int)font.MeasureString("Press A to continue...").X / 2), 600), Color.Black);
                        else spriteBatch.DrawString(font, "Press space to continue...", new Vector2(640-((int)font.MeasureString("Press space to continue...").X/2), 600), Color.Black);
                    }

                    break;
                case gameState.menu:
                    player.draw(spriteBatch);
                    enemy.draw(spriteBatch);
                    player.progressShot(spriteBatch);
                    break;


            }

            spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}
